package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.OriginalBean;
import com.zzyl.entity.ProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.ProjectPlanListMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description NursingPlanServiceIml
 * @Author liangDong
 * @Date 2025-05-08  15:37
 */
@Service
public class NursingPlanServiceIml implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private ProjectPlanListMapper projectPlanListMapper;

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> nursingPlans = nursingPlanMapper.selectAll();
        return nursingPlans;
    }

    @Override
    public void add(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setCreateBy(1L);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setStatus(1);
        nursingPlanMapper.insert(nursingPlan);

        Long planId = nursingPlan.getId();
        List<ProjectPlan> planList = nursingPlanDto.getProjectPlans();
        planList.forEach(pl->{
            pl.setPlanId(planId);
            pl.setCreateBy(1L);
            pl.setCreateTime(LocalDateTime.now());
        });
        //planList.forEach(pl -> pl.setProject_id());
        projectPlanListMapper.insertBatch(planList);

    }

    @Override
    public PageResponse<NursingPlanVo> page(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.page(name,status);
        PageResponse<NursingPlanVo> nursingPlanVo = PageResponse.of(nursingPlans,NursingPlanVo.class);

        List<NursingPlanVo> nursingPlanVoList = nursingPlanVo.getRecords();
        List<Map<String, Object>> countList = nursingPlanMapper.getCountList();
        Map<Long, Integer> countMap = countList.stream().collect(Collectors.toMap(
                map -> ((Number) map.get("id")).longValue(),
                map -> ((Number) map.get("number")).intValue()
        ));
        nursingPlanVoList.forEach(vo ->vo.setCount(countMap.getOrDefault(vo.getId(),0)));
        //nursingPlanMapper.getCount
        return nursingPlanVo;
    }

    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        List<ProjectPlan> nursingPlans = projectPlanListMapper.getByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingPlans);
        return nursingPlanVo;
    }

    @Override
    public void updateById(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setUpdateBy(1L);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlanMapper.update(nursingPlan);

        Long planId = nursingPlanDto.getId();
        //List<ProjectPlan> originalPlanList =  projectPlanListMapper.getByPlanId(planId);
        //List<Long> originalProjectIds =new ArrayList<>();
        //originalPlanList.forEach(opl-> originalProjectIds.add(opl.getProjectId()));


        //获取原计划的项目列表的每个项目的createBy,createTime,projectId。因为先删除列表再新增会丢失创建者信息，
        // projectId负责进行判断新增里面是否有原项目，有就恢复创建者信息以及填写修改信息，没有就是正常新增操作
        List<OriginalBean> originalBeanList = projectPlanListMapper.getOriginalOfProjectPlans(planId);


        //删项目表了
        projectPlanListMapper.delete(planId);
        //获取前端传来的项目列表
        List<ProjectPlan> planList = nursingPlanDto.getProjectPlans();


        //前端的列表不为空才进行新增，否则不操作
        if (!CollectionUtils.isEmpty(planList)){
            planList.forEach(pl->{
                originalBeanList.forEach(obl->{
                    if (obl.getId()==pl.getProjectId()){
                        pl.setPlanId(planId);
                        pl.setCreateBy(obl.getCreateBy());
                        pl.setCreateTime(obl.getCreateTime());
                        pl.setUpdateBy(1L);
                        pl.setUpdateTime(LocalDateTime.now());
                    }else {
                        pl.setPlanId(planId);
                        pl.setCreateTime(LocalDateTime.now());
                        pl.setCreateBy(1L);
                    }
                });

            });
            projectPlanListMapper.insertBatch(planList);
        }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1L);
        nursingPlan.setStatus(status);
        Long planId = Long.valueOf(id);
        nursingPlan.setId(planId);

        //nursingPlanMapper.updateStatus(planId,status);
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public void deleteById(Integer id) {
        //先检查项目与计划的关联表是否存在相关planId，有则删除。
        Long planId = Long.valueOf(id);
        projectPlanListMapper.deleteByPlanId(planId);

        nursingPlanMapper.deleteById(planId);
    }
}
